package com.cskaoyan.controller;

import com.cskaoyan.bean.bo.AdminGoodsCreateBO;
import com.cskaoyan.bean.bo.AdminGoodsDeleteBO;
import com.cskaoyan.bean.bo.AdminGoodsListBO;
import com.cskaoyan.bean.bo.AdminGoodsUpdateBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.AdminGoodsCatAndBrandVO;
import com.cskaoyan.bean.vo.AdminGoodsDetailVO;
import com.cskaoyan.bean.vo.AdminGoodsListVO;
import com.cskaoyan.service.AdminGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;

/**
 * @author zhuqiushi
 * @since 2022/09/06 17:02
 */
@RestController
@RequestMapping("admin/goods")
public class AdminGoodsController {
    @Autowired
    AdminGoodsService adminGoodsService;

    /*
     * /admin/goods/list?page=1&limit=20&goodsSn=111&name=111&sort=add_time&order=desc&goodsId=111
     * get
     * */
    @RequestMapping("list")
    public BaseRespVo<AdminGoodsListVO> list(@Valid AdminGoodsListBO adminGoodsListBO, BindingResult bindingResult) {
        //首先调用service查询
        AdminGoodsListVO adminGoodsListVO = adminGoodsService.getGoodsList(adminGoodsListBO);
        //将查询结果封装返回
        BaseRespVo<AdminGoodsListVO> ok = BaseRespVo.ok(adminGoodsListVO);
        return ok;
    }

    /*
     * /admin/goods/catAndBrand
     * GET
     * */
    @RequestMapping("catAndBrand")
    public BaseRespVo<AdminGoodsCatAndBrandVO> catAndBrand() {
        //首先调用service查询
        AdminGoodsCatAndBrandVO catAndBrandVO = adminGoodsService.getCatAndBrand();
        //将结果返回
        BaseRespVo ok = BaseRespVo.ok(catAndBrandVO);
        return ok;
    }

    /*
     * /admin/goods/create
     * post
     * */
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody AdminGoodsCreateBO createBO) {
        //调用service插入，新增商品
        BaseRespVo<Object> ok = new BaseRespVo<>();
        int code = adminGoodsService.create(createBO);
        if (code == 0) {
            ok.setErrno(0);
            ok.setErrmsg("成功");
        } else if (code == 401) {
            ok.setErrno(401);
            ok.setErrmsg("参数类型错误");
        }
        return ok;
    }

    /*
     * /admin/goods/detail?id=1181009
     * GET
     * */
    @RequestMapping("detail")
    public BaseRespVo<AdminGoodsDetailVO> detail(Integer id) {
        //调用service层查询
        AdminGoodsDetailVO adminGoodsDetailVO = adminGoodsService.getGoodsDetail(id);
        BaseRespVo ok = BaseRespVo.ok(adminGoodsDetailVO);
        ok.setErrmsg("成功");
        ok.setErrno(0);
        return ok;
    }

    /*
     * /admin/goods/update
     * POST
     * */
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody AdminGoodsUpdateBO adminGoodsUpdateBO) {
        //调用service层更改
        int code = adminGoodsService.update(adminGoodsUpdateBO);
        BaseRespVo respVo = new BaseRespVo<>();
        respVo.setErrno(code);
        if (code == 402) {
            respVo.setErrno(402);
            respVo.setErrmsg("参数值不正确");
        } else if (code == 0) {
            respVo = BaseRespVo.ok();
        }
        return respVo;
    }

    /*
     * /admin/goods/delete
     * POST
     * */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody AdminGoodsDeleteBO adminGoodsDeleteBO) {
        //调用service层删除
        int code = adminGoodsService.delete(adminGoodsDeleteBO);
        BaseRespVo respVo = new BaseRespVo<>();
        respVo.setErrno(code);
        if (code == 402) {
            respVo.setErrmsg("参数值不正确");
        } else if (code == 0) {
            respVo = BaseRespVo.ok();
        }
        return respVo;
    }
}
